<?php
namespace zake\helper;


use zake\foundation\Process;

class Console
{

    const COLOR_END             = "\e[0m";

    const COLOR_FONT_GREEN      = "\e[32m";
    const COLOR_FONT_RED        = "\e[31m";
    const COLOR_FONT_YELLOW     = "\e[93m";

    // trace bg
    const COLOR_BG_TRACE        = "\e[44m";
    const COLOR_BG_MASTER        = "\e[41m";


    const SHOW_TRACE = false;



    /**
     * 绿色
     * @param $msg
     * @param string $app
     *
     * @author liu.bin 2020-03-20 23:22
     */
    public static function success($msg,$app="console")
    {
        if(Process::isDaemonize()){
            return;
        }
        $message = self::parse($msg,$app,self::COLOR_FONT_GREEN);
        echo $message;
    }


    /**
     * 红色
     * @param $msg
     * @param string $app
     *
     * @author liu.bin 2020-03-20 23:22
     */
    public static function error($msg,$app="console")
    {
        if(Process::isDaemonize()){
            return;
        }
        $message = self::parse($msg,$app,self::COLOR_FONT_RED);
        echo $message;
    }



    /**
     * 黄色
     * @param $msg
     * @param string $app
     *
     * @author liu.bin 2020-03-20 23:22
     */
    public static function warnning($msg,$app="console")
    {
        if(Process::isDaemonize()){
            return;
        }
        $message = self::parse($msg,$app,self::COLOR_FONT_YELLOW);
        echo $message;
    }


    /**
     * 正常
     * @param $msg
     * @param string $app
     *
     * @author liu.bin 2020-03-20 23:22
     */
    public static function info($msg,$app="console")
    {
        if(Process::isDaemonize()){
            return;
        }
        $message = self::parse($msg,$app);
        echo $message;
    }


    /**
     *
     * @param array | string $list
     * @param string $app
     *
     * @author
     */
    public static function dump($list,$app="console")
    {
        if(Process::isDaemonize()){
            return;
        }
        list($readList,$writeList) = $list;
        $errFlag = false;
        $readMessage = "";
        $writeMessage = "";
        if(!is_array($readList)){
            $readMessage = "【read】list is ".gettype($readList) ." | ";
            $errFlag = true;
        }
        if(!is_array($writeList)){
            $writeMessage = "【write】list is ".gettype($writeList) ." | ";
            $errFlag = true;
        }
        if($errFlag){
            $message = self::parse($readMessage . $writeMessage,$app);
            echo $message;
            return;
        }


        $msg = "read :[ ";
        foreach ($readList as $resource){
            if(is_resource($resource)){
                $msg .= fd($resource) . ".|";
            }else{
                $msg .= "{".gettype($resource)."_".(string)$resource. " _} ";
            }
        }
        $msg = trim($msg,"|");
        $msg .= "] - write :[";
        foreach ($writeList as $resource){
            if(is_resource($resource)){
                $msg .= (int)$resource . ".|";
            }else{
                $msg .= "{".gettype($resource)."_".(string)$resource. " _} ";
            }
        }
        $msg = trim(trim($msg,"|"),".");
        $msg .= "]";
        $message = self::parse($msg,$app);
        echo $message;
    }


    /**
     *
     * @param $msg
     * @param string $app
     * @param string $color
     * @return string
     *
     * @author liu.bin 2020-04-30 10:04
     */
    protected static function parse($msg,$app="console",$color="")
    {
        $trace      = self::showTrace(debug_backtrace());
        $pid        = Process::pid();
        $masterPid  = defined('MASTER_SERVER_PID') ? MASTER_SERVER_PID : 0;
        if($pid == $masterPid && !Process::isDaemonize()){
            $pidArea = static::COLOR_BG_MASTER . "[{$pid}]". static::COLOR_END;
        }else{
            $pidArea = "[{$pid}]";
        }
        $color  = Process::isDaemonize() ? "" : $color;
        $end    = (Process::isDaemonize() || empty($color)) ? "" : static::COLOR_END;
        $msg    = $pidArea ." [".date('i:s')."] [".$app."] " .$color. $msg . $end ."\r\n";
        $msg    .= $trace;
        return $msg;
    }


    /**
     *
     * @param $trace
     * @param bool $show
     * @return string
     * @author
     */
    private static function showTrace($trace)
    {
        $debugInfo = $trace[1]??[];
        if($debugInfo){
            $pre    = '/Users/wyy/webroot';
            $file   = substr($debugInfo['file'],strlen($pre));
            $line   = $debugInfo['line'];
        }else{
            $file   = "not found file";
            $line   = 0;
        }
        $color  = Process::isDaemonize() ? "" : static::COLOR_BG_TRACE;
        $end    = (Process::isDaemonize() || empty($color)) ? "" : static::COLOR_END;
        $traceInfo = "{$color}[{$file}].[{$line}]{$end}\r\n";
        return self::SHOW_TRACE ? $traceInfo : "";
    }


}
